<?php
namespace App\Http\Logic;


use App\Logic\BaoJianMachineLogic;
use App\Logic\DaHuangeMachineLogic;
use App\Models\SwtBjMachine;
use App\Models\SwtDheMachine;
use App\Models\SwtMachine;
use App\Models\SwtOrder;
use App\Models\SwtOrderGoods;
use App\Services\BaoJianService;
use Illuminate\Support\Facades\Cache;

class MachineLogic extends BaseLogic
{

    /**
     * @notes 用户开门状态锁
     * @param $source_type
     * @param $machine_code
     * @param $door_num
     * @param $order_info
     * @param $timeout
     * @return void
     * @author Evil丿
     * @date 2024/09/02 09:10
     */
    public static function setMachineCacheLock($source_type, $machine_code, $door_num, $order_info, $timeout)
    {
        switch ($source_type) {
            case 1:
                Cache::put('open_machine_'.$machine_code.'_'.$door_num.'_'.$order_info['user_id'], json_encode($order_info), $timeout);
            break;
            case 2:
                Cache::put('open_machine_'.$machine_code.'_'.$door_num, json_encode($order_info), $timeout);
            break;
        }
    }

    /**
     * @notes 清除用户开门状态锁
     * @param $order_sn
     * @param $door_num
     * @return void
     * @author Evil丿
     * @date 2024/09/02 09:10
     */
    public static function clearMachineCacheLock($order_sn, $door_num)
    {
        $info = SwtOrder::with('machine') -> firstWhere('order_sn', $order_sn);

        switch ($info -> machine -> source_type) {
            case 1:
                Cache::forget('open_machine_'.$info -> machine -> machine_no.'_'.$door_num .'_'.$info -> user_id);
                break;
            case 2:
                Cache::forget('open_machine_'.$info -> machine -> machine_no.'_'.$door_num);
                break;
        }
    }

    /**
     * @notes 校验设备是否可以购物
     * @param $machine_code
     * @param $door_num
     * @return false
     * @author Evil丿
     * @date 2024/08/30 12:08
     */
    public static function checkMachineSaleStatus($machine_code, $door_num, $user_id)
    {
        $machine_info = self::getMachineInfo($machine_code);

        if($machine_info === false) {
            return false;
        }

        switch($machine_info['source_type']) {
            case 1:
                $res = Cache::has('open_machine_' . $machine_code . '_' . $door_num . '_' . $user_id);
                if($res) {
                    self::$error = "订单未完结";
                    return false;
                }
                $res = DaHuangeMachineLogic::getDoorStatus($machine_info['machine_detail']['device_code']);
                break;
            case 2:
                $res = Cache::has('open_machine_' . $machine_code . '_' . $door_num);
                if($res) {
                    self::$error = "订单未完结";
                    return false;
                }
                $res = BaoJianMachineLogic::getDoorStatus($machine_info['machine_detail']['device_code']);
                break;
            default: //其他未知
                self::$error = "设备供应商未找到";
                $res = false;
            break;
        }

        if(!$res) {
            return false;
        }

        return $machine_info;
    }

    /**
     * @notes 获取设备详情
     * @param $machine_code
     * @return false
     * @author Evil丿
     * @date 2024/08/26 17:16
     */
    public static function getMachineInfo($machine_code)
    {
        $machine_info = SwtMachine::firstWhere('machine_no', $machine_code);

        if(!$machine_info) {
            self::$error = "设备不存在";
            return false;
        }

        if($machine_info -> run_mode == 0) {
            self::$error = "设备已禁用";
            return false;
        }

        if($machine_info -> run_mode == 2) {
            $start = $machine_info -> free_start_time;
            $end = $machine_info -> free_end_time;
            $now = time();
            if($now < $start || $now > $end) {
                self::$error = "设备不在可销售时间内";
                return false;
            }
        }

        switch($machine_info -> source_type) {
            case 1:
                $detail = SwtDheMachine::firstWhere('machine_id', $machine_info -> id);
            break;
            case 2:
                $detail = SwtBjMachine::firstWhere('machine_id', $machine_info -> id);
            break;
        }

        if(!$detail) {
            self::$error = "未找到关联设备";
            return false;
        }

        $machine_info = $machine_info -> toArray();

        $machine_info['machine_detail'] = $detail;

        return $machine_info;
    }

    /**
     * @notes 设备开门
     * @param $merchant
     * @param $params
     * @return boolean
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/07/29 16:35
     */
    public static function openDoor($order_info)
    {
        switch($order_info['machine']['source_type'])
        {
            case 1 :
                $res = DaHuangeMachineLogic::openDoor($order_info);
                break;
            case 2:
                $res = BaoJianMachineLogic::openDoor($order_info);
                break;
            default:
                self::$error = "厂商未找到";
                $res = false;
                break;
        }

        if($res === false) {
            self::$error = "设备开门失败";
            return false;
        }

        return true;
    }

    /**
     * @notes 设备增加收益及销售额
     * @param $machine_id
     * @param $order_id
     * @return void
     * @author Evil丿
     * @date 2024/08/07 16:05
     */
    public static function addMachineAmountByOrderId($machine_id, $order_id)
    {
        $goods_list = SwtOrderGoods::where('order_id', $order_id) -> get();

        if(!empty($goods_list)) {
            $total_amount = 0;

            foreach($goods_list as $item) {
                $total_amount += $item['goods_price'];
            }

            SwtMachine::where('id', $machine_id) -> increment('total_amount', $total_amount);
        }
    }
}
